﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SoundEdge.Media.CoreAudioApi;
using SoundEdge.Media.Settings;

namespace SoundEdge.WinControls.Settings
{
    public partial class WasapiOutSettingsPanel : UserControl
    {
        private WasapiOutSettings _settings;

        public WasapiOutSettingsPanel(WasapiOutSettings settings)
        {
            _settings = settings;
            InitializeComponent();
            InitialiseWasapiControls();
        }

        class WasapiDeviceComboItem
        {
            public string Description { get; set; }
            public MMDevice Device { get; set; }
        }

        private void InitialiseWasapiControls()
        {
            var enumerator = new MMDeviceEnumerator();
            var endPoints = enumerator.EnumerateAudioEndPoints(DataFlow.Render, DeviceState.Active);
            var comboItems = new List<WasapiDeviceComboItem>();
            foreach (var endPoint in endPoints)
            {
                var comboItem = new WasapiDeviceComboItem();
                comboItem.Description = string.Format("{0} ({1})", endPoint.FriendlyName, endPoint.DeviceFriendlyName);
                comboItem.Device = endPoint;
                comboItems.Add(comboItem);
            }
            comboBoxWaspai.DisplayMember = "Description";
            comboBoxWaspai.ValueMember = "Device";
            comboBoxWaspai.DataSource = comboItems;

            WasapiDeviceComboItem selected = null;
            if (_settings != null)
            {
                selected = comboBoxWaspai.Items.OfType<WasapiDeviceComboItem>().FirstOrDefault(item => item.Device.ID == _settings.MMDeviceId);
            }

            comboBoxWaspai.SelectedIndex = selected == null ? 0 : comboBoxWaspai.Items.IndexOf(selected);
            checkBoxWasapiEventCallback.Checked = _settings.EventCallback;
            checkBoxWasapiExclusiveMode.Checked = _settings.ShareMode == AudioClientShareMode.Exclusive;
        }

        public MMDevice SelectedDevice { get { return (MMDevice)comboBoxWaspai.SelectedValue; } }

        public AudioClientShareMode ShareMode
        {
            get
            {
                return checkBoxWasapiExclusiveMode.Checked ?
                    AudioClientShareMode.Exclusive :
                    AudioClientShareMode.Shared;
            }
        }

        public bool UseEventCallback { get { return checkBoxWasapiEventCallback.Checked; } }

        private void comboBoxWaspai_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (_settings == null || comboBoxWaspai.SelectedValue == null)
            {
                return;
            }
            _settings.MMDeviceId = SelectedDevice.ID;
        }

        private void checkBoxWasapiEventCallback_CheckedChanged(object sender, EventArgs e)
        {
            if (_settings == null)
            {
                return;
            }
            _settings.EventCallback = checkBoxWasapiEventCallback.Checked;
        }

        private void checkBoxWasapiExclusiveMode_CheckedChanged(object sender, EventArgs e)
        {
            if (_settings == null)
            {
                return;
            }
            _settings.ShareMode = ShareMode;
        }
    }
}
